package com.orange.adminapi.controller.config;

import com.orange.core.pojo.R;
import com.orange.core.pojo.req.config.ConfigUpdateReq;
import com.orange.core.service.config.ConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 配置
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/config")
public class ConfigController {

    private final ConfigService configService;

    /**
     * 获取积分兑换比率
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_CREDIT_RATE.permission.identifier)")
    @GetMapping("/creditsRate")
    public R<Integer> creditsRate() {
        return R.ok(configService.getCreditsRate());
    }

    /**
     * 设置积分兑换比率
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_CREDIT_RATE_UPDATE.permission.identifier)")
    @PostMapping("/creditsRate")
    public R<Boolean> creditsRate(@RequestBody @Validated ConfigUpdateReq req) {
        configService.setCreditsRate(Integer.parseInt(req.getValue()));
        return R.ok();
    }

    /**
     * 获取首次注册获得积分
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_REGISTER_CREDIT.permission.identifier)")
    @GetMapping("/firstRegisterCredit")
    public R<Integer> firstRegisterCredit() {
        return R.ok(configService.getFirstRegisterCredit());
    }

    /**
     * 设置首次注册获得积分
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_REGISTER_CREDIT_UPDATE.permission.identifier)")
    @PostMapping("firstRegisterCredit")
    public R<Boolean> firstRegisterCredit(@RequestBody @Validated ConfigUpdateReq req) {
        configService.setFirstRegisterCredit(Integer.parseInt(req.getValue()));
        return R.ok();
    }

    /**
     * 积分兑换停车时长
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_PARK_TIME_RATE.permission.identifier)")
    @GetMapping("/creditParkTimeRate")
    public R<Integer> creditParkTimeRate() {
        return R.ok(configService.getCreditParkTimeRate());
    }

    /**
     * 设置积分兑换停车时长
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_PARK_TIME_RATE_UPDATE.permission.identifier)")
    @PostMapping("creditParkTimeRate")
    public R<Boolean> creditParkTimeRate(@RequestBody @Validated ConfigUpdateReq req) {
        configService.setCreditParkTimeRate(Integer.parseInt(req.getValue()));
        return R.ok();
    }

    /**
     * 积分兑换停车金额
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_PARK_AMOUNT_RATE.permission.identifier)")
    @GetMapping("/creditParkAmountRate")
    public R<BigDecimal> creditParkAmountRate() {
        return R.ok(configService.getCreditParkAmountRate());
    }

    /**
     * 设置积分兑换停车金额
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_PARK_AMOUNT_RATE_UPDATE.permission.identifier)")
    @PostMapping("creditParkAmountRate")
    public R<Boolean> creditParkAmountRate(@RequestBody @Validated ConfigUpdateReq req) {
        configService.setCreditParkAmountRate(new BigDecimal(req.getValue()));
        return R.ok();
    }

    /**
     * 免费停车时长
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_FREE_PARK_TIME.permission.identifier)")
    @GetMapping("/freeParkTime")
    public R<Integer> freeParkTime() {
        return R.ok(configService.getFreeParkTime());
    }

    /**
     * 设置免费停车时长
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CONFIG_FREE_PARK_TIME_UPDATE.permission.identifier)")
    @PostMapping("freeParkTime")
    public R<Boolean> freeParkTime(@RequestBody @Validated ConfigUpdateReq req) {
        configService.setFreeParkTime(Integer.parseInt(req.getValue()));
        return R.ok();
    }
}
